package com.zjl.workflow.service;

import com.sun.webkit.dom.EntityImpl;
import com.zjl.workflow.entity.Person;
import com.zjl.workflow.repository.PersonRepository;
import com.zjl.workflow.repository.mapper.ActHiProcinstMapper;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.flowable.engine.impl.persistence.entity.data.impl.MybatisHistoricProcessInstanceDataManager;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: zhuangjl
 * @Date: 2021/11/24 14:41
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MyService {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private TaskService taskService;

    @Resource
    private RepositoryService repositoryService;

    @Resource
    private HistoryService historyService;

    @Resource
    private ActHiProcinstMapper actHiProcinstMapper;

    @Transactional
    public void startProcess(String assignee) {
        Person person = personRepository.findByUsername(assignee);
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("person", person);
        ProcessInstance oneTaskProcess = runtimeService.startProcessInstanceByKey("oneTaskProcess", variables);

    }

    @Transactional
    public void startHoliday(String assignee) {
        //参数
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("employee", "zhuangjl");
        variables.put("nrOfHolidays", 3);
        variables.put("description", "测试");
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("holidayRequest3", variables);


    }

    @Transactional
    public void startFinanceInstance() {

        runtimeService.startProcessInstanceByKey("financialReport");
    }

    @Transactional
    public List<Task> getTasks(String assignee) {

        return taskService.createTaskQuery().taskAssignee(assignee).list();

    }

    @Transactional
    public List<Task> getFinanceTasks(String username) {

        return taskService.createTaskQuery().taskCandidateUser(username).list();
    }

    public void createDemoUsers() {
        if (personRepository.list().size() == 0) {
            personRepository.save(new Person("zhuangjl"));
            personRepository.save(new Person("huangjk"));
        }
    }

    public void updateInstHis(String instanceId,String title) {
        actHiProcinstMapper.updateProcinstNameById(instanceId,title);
    }
}
